package com.suda.controller;

import com.github.pagehelper.PageInfo;
import com.suda.pojo.Warehouse;
import com.suda.service.WarehouseService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/****
 * @Author:zhuyongjie
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/warehouse")
@CrossOrigin
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;

    /***
     * Warehouse分页条件搜索实现
     * @param warehouse
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) Warehouse warehouse, @PathVariable  int page, @PathVariable  int size){
        //调用WarehouseService实现分页条件查询Warehouse
        PageInfo<Warehouse> pageInfo = warehouseService.findPage(warehouse, page, size);
        return new Result(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Warehouse分页搜索实现
     * @param page:当前页
     * @param limit:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search" )
    public Result<PageInfo> findPage(int page, int limit){
        //调用WarehouseService实现分页查询Warehouse
        PageInfo<Warehouse> pageInfo = warehouseService.findPage(page, limit);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param warehouse
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Warehouse>> findList(@RequestBody(required = false) Warehouse warehouse){
        //调用WarehouseService实现条件查询Warehouse
        List<Warehouse> list = warehouseService.findList(warehouse);
        return new Result<List<Warehouse>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用WarehouseService实现根据主键删除
        warehouseService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Warehouse数据
     * @param warehouse
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody Warehouse warehouse, @PathVariable String id){
        //设置主键值
        warehouse.setWarehouseId(id);
        //调用WarehouseService实现修改Warehouse
        warehouseService.update(warehouse);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Warehouse数据
     * @param warehouse
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Warehouse warehouse){
        //调用WarehouseService实现添加Warehouse
        warehouseService.add(warehouse);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Warehouse数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Warehouse> findById(@PathVariable String id){
        //调用WarehouseService实现根据主键查询Warehouse
        Warehouse warehouse = warehouseService.findById(id);
        return new Result<Warehouse>(true,StatusCode.OK,"查询成功",warehouse);
    }

    /***
     * 查询Warehouse全部数据
     * @return
     */
    @GetMapping
    public Result<List<Warehouse>> findAll(){
        //调用WarehouseService实现查询所有Warehouse
        List<Warehouse> list = warehouseService.findAll();
        return new Result<List<Warehouse>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
